home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / RAVLMap.hP < prev    next >
Text File  |  1992-05-29  |  4KB  |  148 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu) 
  5.     ranking code from Paul Anderson (paul%lfcs.ed.ac.uk)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20.  
  21. #ifndef _<T><C>RAVLMap_h
  22. #ifdef __GNUG__
  23. #pragma interface
  24. #endif
  25. #define _<T><C>RAVLMap_h 1
  26.  
  27. #include "<T>.<C>.Map.h"
  28.  
  29. struct <T><C>RAVLNode
  30. {
  31.   <T><C>RAVLNode*      lt;
  32.   <T><C>RAVLNode*      rt;
  33.   <T>                 item;
  34.   <C>                 cont;
  35.   int                 rank;
  36.   char                stat;
  37.                       <T><C>RAVLNode(<T&> h, <C&> c, 
  38.                            <T><C>RAVLNode* l=0, <T><C>RAVLNode* r=0, int k=1);
  39.                       ~<T><C>RAVLNode();
  40. };
  41.  
  42. inline <T><C>RAVLNode::<T><C>RAVLNode(<T&> h, <C&> c, 
  43.                            <T><C>RAVLNode* l, <T><C>RAVLNode* r, int k)
  44.      :item(h), cont(c), lt(l), rt(r), rank(k), stat(0) {}
  45.  
  46. inline <T><C>RAVLNode::~<T><C>RAVLNode() {}
  47.  
  48. typedef <T><C>RAVLNode* <T><C>RAVLNodePtr;
  49.  
  50.  
  51. class <T><C>RAVLMap : public <T><C>Map
  52. {
  53. protected:
  54.   <T><C>RAVLNode*   root;
  55.  
  56.   <T><C>RAVLNode*   leftmost();
  57.   <T><C>RAVLNode*   rightmost();
  58.   <T><C>RAVLNode*   pred(<T><C>RAVLNode* t);
  59.   <T><C>RAVLNode*   succ(<T><C>RAVLNode* t);
  60.   void            _kill(<T><C>RAVLNode* t);
  61.   void            _add(<T><C>RAVLNode*& t);
  62.   void            _del(<T><C>RAVLNode* p, <T><C>RAVLNode*& t);
  63.  
  64. public:
  65.                 <T><C>RAVLMap(<C&> dflt);
  66.                 <T><C>RAVLMap(<T><C>RAVLMap& a);
  67.                 ~<T><C>RAVLMap();
  68.  
  69.   <C>&          operator [] (<T&> key);
  70.  
  71.   void          del(<T&> key);
  72.  
  73.   Pix           first();
  74.   void          next(Pix& i);
  75.   <T>&          key(Pix i);
  76.   <C>&          contents(Pix i);
  77.  
  78.   Pix           seek(<T&> key);
  79.   int           contains(<T&> key);
  80.  
  81.   Pix           ranktoPix(int i);
  82.   int           rankof(<T&> key);
  83.  
  84.   void          clear(); 
  85.  
  86.   Pix           last();
  87.   void          prev(Pix& i);
  88.  
  89.   int           OK();
  90. };
  91.  
  92. inline <T><C>RAVLMap::~<T><C>RAVLMap()
  93. {
  94.   _kill(root);
  95. }
  96.  
  97. inline <T><C>RAVLMap::<T><C>RAVLMap(<C&> dflt) :<T><C>Map(dflt)
  98. {
  99.   root = 0;
  100. }
  101.  
  102.  
  103. inline Pix <T><C>RAVLMap::first()
  104. {
  105.   return Pix(leftmost());
  106. }
  107.  
  108. inline Pix <T><C>RAVLMap::last()
  109. {
  110.   return Pix(rightmost());
  111. }
  112.  
  113. inline void <T><C>RAVLMap::next(Pix& i)
  114. {
  115.   if (i != 0) i = Pix(succ((<T><C>RAVLNode*)i));
  116. }
  117.  
  118. inline void <T><C>RAVLMap::prev(Pix& i)
  119. {
  120.   if (i != 0) i = Pix(pred((<T><C>RAVLNode*)i));
  121. }
  122.  
  123. inline <T>& <T><C>RAVLMap::key(Pix i)
  124. {
  125.   if (i == 0) error("null Pix");
  126.   return ((<T><C>RAVLNode*)i)->item;
  127. }
  128.  
  129. inline <C>& <T><C>RAVLMap::contents(Pix i)
  130. {
  131.   if (i == 0) error("null Pix");
  132.   return ((<T><C>RAVLNode*)i)->cont;
  133. }
  134.  
  135. inline void <T><C>RAVLMap::clear()
  136. {
  137.   _kill(root);
  138.   count = 0;
  139.   root = 0;
  140. }
  141.  
  142. inline int <T><C>RAVLMap::contains(<T&> key)
  143. {
  144.   return seek(key) != 0;
  145. }
  146.  
  147. #endif
  148.